<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" >
      <el-form-item>
        <el-input v-model="dataForm.nickName" placeholder="请输入昵称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="请输入手机账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.regFrom" placeholder="请输入注册来源" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="dateArr" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
          value-format='yyyy-MM-dd HH:mm:ss'>
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="resetting()">重置</el-button>
        <!-- <el-button v-if="isAuth('course:coursetype:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="序号"
        width="50"
      >
      <template slot-scope="scope">
          <div v-if="scope.row">
            {{ scope.$index+1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="phoneNumber" header-align="center" align="center" label="手机号码">
      </el-table-column>
      <el-table-column prop="nickName" header-align="center" align="center" label="昵称" width="150">
      </el-table-column>
      <!-- <el-table-column prop="superman" header-align="center" align="center" label="用户类型" width="100">
        <template slot-scope="scope">
        <div v-if="scope.row">
            <span v-if="scope.row.superman == 1">超级用户</span>
            <span v-else>普通用户</span>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column prop="regDate" header-align="center" align="center" label="注册时间" width="180">
        <template slot-scope="scope">
          <div v-if="scope.row">
            {{ getStrTime(scope.row.regDate) }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="name" header-align="center" align="center" label="归属员工">
      </el-table-column>
      <el-table-column prop="sjbAccount" header-align="center" align="center" label="世界波账号">
      </el-table-column>
      <el-table-column prop="regFrom" header-align="center" align="center" label="注册来源">
      </el-table-column>
      <el-table-column prop="score" header-align="center" align="center" label="积分余额">
      </el-table-column>
      <el-table-column prop="categoryName" header-align="center" align="center" label="访问权限">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="getCategoryName(scope.row.id)">查看</el-button>
        </template>
      </el-table-column> -->
      <el-table-column prop="updateTime" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <div v-if="scope.row">
            <el-button type="success" v-if="scope.row.status == 1">正常</el-button>
            <el-button type="danger" v-else>禁用</el-button>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="type" header-align="center" align="center" label="账号类型">
        <template slot-scope="scope">
          <div v-if="scope.row">
            <el-button type="success" v-if="scope.row.type == 1">内部</el-button>
            <el-button type="danger" v-else>外部</el-button>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="details(scope.row.id)">查看</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)" v-if="isAuth('user:update')">编辑</el-button>
          <el-button type="text" size="small" @click="uStatus(scope.row.id, 0)" v-if="scope.row.status == 1&&isAuth('user:disable')">禁用</el-button>
          <el-button type="text" size="small" @click="uStatus(scope.row.id, 1)" v-if="scope.row.status == 0&&isAuth('user:enable')">启用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <AddOrUpdates v-if="addOrUpdateVisibles" ref="addOrUpdates" @refreshDataList="getDataList"></AddOrUpdates>
    <el-dialog title="批量设置" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="dataForm" label-width="80px">
        <el-form-item label="会员状态">
          <el-select v-model="memberStatus" placeholder="请选择">
            <el-option v-for="item in option1" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="cmtSet()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="关系网" :visible.sync="dialogVisible2" width="30%" :before-close="handleClose2">
      <div>
        <el-table ref="multipleTable" :data="categoryList" tooltip-effect="dark" style="width: 100%">
          <el-table-column label="序号" width="120">
            <template slot-scope="scope">{{ scope.$index+1 }}</template>
          </el-table-column>
          <el-table-column label="权限" width="120">
            <template slot-scope="scope">{{ scope.row.categoryName }}</template>
          </el-table-column>
          <el-table-column prop="name" label="操作" width="120">
            <template slot-scope="scope">
            <el-button type="text" size="small" @click="rm(scope.row.id)" v-if="isAuth('user:category:update')">删除</el-button>
          </template>
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from './memberList-add-or-update'
import AddOrUpdates from './details'

export default {
  data () {
    return {
      categoryList: [],
      dialogVisible2: false,
      dateArr: [],
      dialogVisible: false,
      dataForm: {
        nickName: '',
        regFrom: '',
        key: '',
        status: null,
        type: null
      },
      memberStatus: null,
      options: [
        {
          value: null,
          label: '全部'
        },
        {
          value: 1,
          label: '正常'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      option1: [
        {
          value: 1,
          label: '正常'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      optionTwo: [
        {
          value: null,
          label: '全部'
        },
        {
          value: 1,
          label: '内部'
        },
        {
          value: 2,
          label: '外部'
        }
      ],
      qxId: '',
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addOrUpdateVisibles: false
    }
  },
  components: {
    AddOrUpdate,
    AddOrUpdates
  },
  mounted () {
    this.$forceUpdate()
  },
  activated () {
    this.getDataList()
  },
  methods: {
    handleClose2 () {
      this.dialogVisible2 = false
    },
    cmtSet () {
      if (this.dataListSelections.length > 0) {
        var ids = this.dataListSelections.map((item, index) => {
          return item.memberId
        })
        this.uStatus(ids, this.memberStatus)
      } else {
        this.$message.error('请选择至少一个用户')
      }
    },
    handleClose (done) {
      done()
    },
    rm (id) {
      this.$http({
        url: this.$http.adornUrl(`/admin/user/category/update`),
        method: 'post',
        data: this.$http.adornData({
          id: id
        })
      }).then(({ data }) => {
        if (data && data.code === '200') {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getCategoryList()
                // this.dialogVisible2 = false
            }
          })
        } else {
        }
      })
    },
    resetting () {
      this.dataForm = {
        nickName: '',
        regFrom: '',
        key: '',
        status: null
      }
      this.dateArr = []
      this.getDataList()
    },
    // 获取数据列表
    getDataList () {
      var status = {}
      this.dataListLoading = true
      if (this.dataForm.status !== null) {
        status.status = this.dataForm.status
      }
      let params = {
      }
      if (this.dateArr && this.dateArr.length > 0) {
        params.startTime = this.dateArr[0]
        params.endTime = this.dateArr[1]
      }
      this.$http({
        url: this.$http.adornUrl(`/admin/user/page`),
        method: 'post',
        data: this.$http.adornData({
          nickName: this.dataForm.nickName,
          regFrom: this.dataForm.regFrom,
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          phoneNumber: this.dataForm.key,
          ...status,
          ...params
        })
      }).then(({ data }) => {
        if (data && data.code === '200') {
          this.dataList = data.list
          console.log(this.dataList)
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    details (id) {
      console.log(id)
      this.addOrUpdateVisibles = true
      this.$nextTick(() => {
        this.$refs.addOrUpdates.init(id)
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (info) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(info)
      })
    },
    getCategoryName (id) {
      this.qxId = id
      this.getCategoryList()
      this.dialogVisible2 = true
    },
    getCategoryList (id) {
      this.$http({
        url: this.$http.adornUrl(`/admin/user/category/list?id=` + this.qxId),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data && data.code === '200') {
          this.categoryList = data.list
        }
      })
    },
    // 删除
    uStatus (id, e) {
      this.$confirm(
        `确定要 ${e == 1
          ? '启用？启用后将恢复登录前端平台权限'
          : '禁用？禁用后会员将无法登录前端平台，数据不受影响'
        }`,
        `会员${e == 1 ? '启用' : '禁用'} `,
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'danger'
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl(e == 1 ? '/admin/user/status/enable' : '/admin/user/status/disable'),
          method: 'post',
          data: this.$http.adornData({ id: id, status: e }, false)
        }).then(({ data }) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
                this.dialogVisible = false
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.info {
  display: flex;
  justify-content: center;
  align-items: center;

  .img {
    width: 40px;
    height: 40px;
    border: 1px solid #999;
    border-radius: 50%;
    overflow: hidden;
    margin-right: 10px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .rightCo {
    .title {
      color: #333;
      font-size: 14px;
    }

    .phone {
      color: #333;
      font-size: 14px;
    }
  }
}
</style>
